MsgN("-- commands.lua loaded")

local function IsInWater( pos )
	local trace = {}
	trace.start = pos
	trace.endpos = pos + Vector(0,0,1)
	trace.mask = MASK_WATER | MASK_SOLID

	local tr = util.TraceLine(trace)

	return tr.Hit
end

--this function creates a plant that will grow
function MDRP.DoPlant( ply, cmd, args )


	local tr = ply:GetEyeTrace()

	if (!tr.HitWorld) then
		ply:EasyMessage( "Aim at the ground to plant." )
		return
	end

	local seed = args[1]

	if (!seed) or (!MDRP.Seeds[seed]) then
		ply:EasyMessage( "Sorry the seed is not valid (" .. seed.. ")." )
		return
	end



--[[	if (!ply:HasInventory(seed)) then
		ply:EasyMessage( "You need an " .. seed.. " to be able to plant." )
		return
	end
]]
	local ground = MDRP.Materials[tr.MatType]

	--check to make sure conditions are good
	if (ground == "Dirt" or ground == "Grass" or ground == "Sand") and (!IsInWater(tr.HitPos)) then
		--if !IsTouching( tr.HitPos, "gms_seed", 30 ) and !ClassIsNearby( tr.HitPos, "prop_physics", 50 ) then

		if (IsTouching( tr.HitPos, "mdrp_base", 30 )) then
			ply:EasyMessage( "You need more distance between seeds/props." )
		else
			if (ply:GetItemCount(seed) >= (MDRP.Limits[seed] or 10)) then
				ply:EasyMessage( "You have reached your limit for planting of this seed." )
			else
				CreateEntity( ply, tr.HitPos, seed, (seed != "Rock") )
			end
		end
	else
		ply:EasyMessage( "You cannot plant on this terrain." )
	end

end

concommand.Add("mds_plant", MDRP.DoPlant)




function MDRP.LoadCharacter( ply )
	ply:Load()

	ply:EasyMessage("Character Restored!")
end
concommand.Add("mdrp_loadcharacter", MDRP.PlayerInitialSpawn)

function MDRP.SaveCharacter( ply )
	ply:Save()

	ply:EasyMessage("Character Saved!")
end
concommand.Add("mdrp_savecharacter", MDRP.PlayerDisconnected)

function MDRP.GetSizeTest( ply, cmd, args )
	local tr = ply:GetEyeTrace()
	local ent = tr.Entity

	if (!ent) or (!ent:IsValid()) then return end

	local max = ent:OBBMaxs()
	local min = ent:OBBMins()

	MsgN("ent:" .. (max-min):Length())
end
concommand.Add("mdrp_getsize", MDRP.GetSizeTest)





--this function ignites a prop
function MDRP.StartFire( ply, cmd, args )
	local tr = ply:GetEyeTrace()
	local ent = tr.Entity

	if (!ent) or (!ent:IsValid()) then return end

	ent:Ignite()
end
concommand.Add("mdrp_startfire", MDRP.StartFire)



--this function ignites a prop
function MDRP.StopFire( ply, cmd, args )
	local tr = ply:GetEyeTrace()
	local ent = tr.Entity

	if (!ent) or (!ent:IsValid()) then return end

	ent:Extinguish()
end
concommand.Add("mdrp_stopfire", MDRP.StopFire)